// -------------------------------------------------------------------------
//    @FileName         :    {Filename}
//    @Author           :    GaoYi
//    @Date             :    2017-09-27
//    @Module           :   {Filename}
//
// -------------------------------------------------------------------------

#include "NFTimerTest.h"
#include "NFComm/NFPluginModule/NFIKernelModule.h"
#include "NFComm/NFPluginModule/NFILogModule.h"
#include "NFComm/NFCore/NFProfiler.h"
#include "NFComm/NFCore/NFCpu.h"
#include "NFComm/NFMessageDefine/NFProtocolDefine.hpp"

bool NFCTimerTestModule::Init()
{
    m_pKernelModule = pPluginManager->FindModule<NFIKernelModule>();
    m_pLogModule = pPluginManager->FindModule<NFILogModule>();
    m_pTimerModule = pPluginManager->FindModule<NFITimerModule>();
    return true;
}

bool NFCTimerTestModule::Execute()
{
	static uint64_t cpuTime = NFGetTime();
	
	if (NFGetTime() - cpuTime >= 500)
	{
		cpuTime = NFGetTime();
		std::ostringstream os;
		//int cpuUseage = NFCpu::GetCurCpuUseage();
		int memSize = NFCpu::GetCurMemorySize();

		int memSize_k = (memSize % (1024 * 1024)) / 1024;
		int memSize_m = memSize / (1024*1024);
		os << "cur process mem size:" << memSize_m << "m" << "," << memSize_k << "k" << std::endl;
		m_pLogModule->LogNormal(NFILogModule::NLL_INFO_NORMAL, NFGUID(), os);

		m_pKernelModule->CreateObject(NFGUID(), NFrame::Player::ThisName(), "0", NFDataList());
	}

//     BEGIN_PROFILE("TimerModule.Execute");
//     m_pTimerModule->Execute();
//     END_PROFILE();
// 
//     static bool Run = false;
//     if (Run)
//     {
//         return true;
//     }
//     Run = true;
// 
//     Test();
    return true;
}

void NFCTimerTestModule::Test()
{
    NFGUID guid = m_pKernelModule->CreateGUID();
    OBJECT_TIMER_FUNCTOR functor = std::bind(&NFCTimerTestModule::ObjectFunctor, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
    //m_pTimerModule->SetTimer(guid, 0, functor, 32, 10);
    //m_pTimerModule->SetTimer(guid, 1, functor, 60, 10);
    //m_pTimerModule->SetTimer(guid, 2, functor, 120, 10);
    //m_pTimerModule->SetTimer(guid, 3, functor, 240, 10);
    //m_pTimerModule->SetTimer(guid, 4, functor, 300, 10);
    //m_pTimerModule->SetTimer(guid, 5, functor, 3000, 10);

    //m_pTimerModule->SetTimer(guid, 7, functor, 300000, 10);

    BEGIN_PROFILE("10000 set timer");

    NFGUID guid = m_pKernelModule->CreateGUID();
    for (int i = 1; i < 10001; i++)
    {
        if (i % 10 == 0)
        {
            guid = m_pKernelModule->CreateGUID();
        }

        uint32_t grid = i % 10;
        uint32_t t = std::rand() % 10000 + 1;

        m_pTimerModule->SetTimer(guid, grid, functor, t, 1);
    }

    m_pTimerModule->SetTimer(guid, 11, functor, 10000, 10);
    END_PROFILE();

    lastTick = NFGetTime();
}

int NFCTimerTestModule::ObjectFunctor(const NFGUID& self, const uint32_t nTimerId, const uint64_t nInterVal, const uint32_t nCallCount)
{
    //     uint64_t now = NFGetTime();
    //     std::cout << "use time:" << now - lastTick << " timeId:"
    //               << nTimerId << " interVal:" << nInterVal
    //               << " callCount:" << nCallCount << std::endl;
    //     lastTick = now;
    //  uint64_t t = std::rand() % 10000 + 1;
    //  int id = std::rand() % 10;
    OBJECT_TIMER_FUNCTOR functor = std::bind(&NFCTimerTestModule::ObjectFunctor, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
    //m_pTimerModule->SetTimer(self, (uint32_t)(nTimerId + id), functor, t, 1);
    //
    //  m_pTimerModule->KillTimer(self, id);
    //  loop++;
    if (nTimerId == 11)
    {
        //m_pTimerModule->SetTimer(self, 12, functor, 10000, 1);
        //m_pTimerModule->KillAllTimer();
        std::cout << "loop 10000" << std::endl;
        uint64_t now = NFGetTime();
        std::cout << "use time:" << now - lastTick << " timeId:"
                  << nTimerId << " interVal:" << nInterVal
                  << " callCount:" << nCallCount << std::endl;

        lastTick = now;
    }

    return 0;
}